package com.zbl.zblproject.hrm.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zbl.zblproject.core.entity.Result;
import com.zbl.zblproject.core.utils.ResultUtil;
import com.zbl.zblproject.hrm.entity.HrmDepartment;
import com.zbl.zblproject.hrm.service.HrmEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author youlanqiang
 * @version 1.0
 * @date 2018/10/29
 */
@RestController
@Api(tags = "部门接口")
@RequestMapping("/department")
public class HrmDepartmentController {

    @Autowired
    private HrmEmployeeService hrmEmployeeService;


    @PostMapping("/add")
    @ApiOperation("添加一个部门")
    public Result add(HrmDepartment department) {
        if (hrmEmployeeService.addDepartment(department)) {
            return ResultUtil.success("添加成功!");
        }
        return ResultUtil.error("添加失败!");
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除一个部门")
    public Result delete(@ApiParam("部门ID") int id) {
        if (hrmEmployeeService.deleteDepartmentById(id)) {
            return ResultUtil.success("删除成功!");
        }
        return ResultUtil.error("删除失败");
    }

    @PostMapping("/update")
    @ApiOperation("更新部门")
    public Result update(HrmDepartment department) {
        if (department.getId() <= 0) {
            return ResultUtil.error("部门ID异常!");
        }
        if (hrmEmployeeService.updateDepartmentById(department)) {
            return ResultUtil.success("更新成功!");
        }
        return ResultUtil.error("更新失败!");
    }

    @GetMapping("/all")
    @ApiOperation("获取所有的部门信息")
    public Result all() {
        return ResultUtil.success("成功!", hrmEmployeeService.allDepartment());
    }


    @GetMapping("/all-list")
    @ApiOperation("所有的部门信息列表")
    public Result allList() {
        return ResultUtil.success("成功!", hrmEmployeeService.allDepartmentList());
    }

    @GetMapping("/page/{current}/{size}")
    @ApiOperation("分页")
    public Result page(@ApiParam("当前页面") @PathVariable int current,
                       @ApiParam("项数") @PathVariable int size,
                       @ApiParam("部门名称") @RequestParam("name") String name) {

        if (current <= 0 || size <= 0) {
            return ResultUtil.error("分页参数错误!");
        }

        Page<HrmDepartment> page = new Page<>(current, size);
        return ResultUtil.success("成功!", hrmEmployeeService.pageDepartment(page, name));
    }


}
